<?php
// +----------------------------------------------------------------------
// | 文章分类控制器
// +----------------------------------------------------------------------
// | Author: Eric <ieyangxiaoguo@126.com>
// +----------------------------------------------------------------------
// | Copyright ©2017 http://www.zzuyxg.top All rights reserved.
// +----------------------------------------------------------------------
// | 2017年10月10日
// +----------------------------------------------------------------------
namespace app\tdadmin\controller;

use app\common\controller\AdminBaseController;
use app\common\model\CollegeModel;
use app\common\model\QuestionsModel;
use think\Db;
use think\Validate;

class QuestionController extends AdminBaseController
{
    /**
     * 学院列表
     */
    public function index()
    {
        return $this->fetch();
    }

    /**
     * 分类列表--数据表格数据接口
     * @author Eric ieyangxiaoguo@126.com
     * @param int $trashed 默认0，过滤已删除的分类；所有分类
     * @return mixed
     * @throws \think\Exception\DbException
     */
    public function getQuestion()
    {
        $model = new QuestionsModel();

        $categorys = $model->alias('a')->all();
        $res['code'] = 0;
        $res['msg'] = '';
        $res['data'] = $categorys;

        return $res;
    }

    /**
     * 添加分类
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();

            $rules = [
                'question' => 'unique:Questions,question',
            ];
            $messages = [
                'question.unique' => '问题已存在',
            ];
            $validate = new Validate($rules, $messages);

            if ($validate->check($data)) {
                $res['code'] = 1;
                $res['msg'] = '添加成功！';
                $model = new QuestionsModel();
                $result = $model->insertGetId($data);
                if ($result === false) {
                    $res['code'] = 0;
                    $res['msg'] = '添加失败！';
                }
                addLog("添加了新问题",Db::getLastSql());
                return $res;
            } else {
                $res['code'] = 0;
                $res['msg'] = $validate->getError();
                return $res;
            }
        } else {
            return $this->fetch();
        }
    }

    /**
     * 编辑分类
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $res['code'] = 1;
            $res['msg'] = '添加成功！';
            $model = new QuestionsModel();
            $result = $model->where('id', $data['id'])->update($data);
            if ($result === false) {
                $res['code'] = 0;
                $res['msg'] = '修改失败！';
            } else {
                $res['code'] = 1;
                $res['msg'] = '修改成功！';
            }

            addLog("修改了问题ID:".$data['id'],Db::getLastSql());

            return $res;

        } else {
            $id = $this->request->param('id', 0, 'intval');
            if ($id > 0) {
                $question = QuestionsModel::where('id', $id)->find();
                $this->assign('question', $question);
                return $this->fetch();
            } else {
                $res['code'] = 0;
                $res['msg'] = '你从哪里来？';
                return $res;
            }
        }
    }

    //检测分类是否重复
    public function checkName()
    {
        $name = $this->request->param('name');

        $data['code'] = 1;
        $data['msg'] = '该名称可用！';

        if (CollegeModel::where('college_name', $name)->find()) {
            $data['code'] = 0;
            $data['msg'] = '该分类已存在';
        }

        return $data;
    }

    /**
     * 显示/开启操作
     *
     * @author Eric ieyangxiaoguo@126.com
     * @date 2017年11月26日
     */
    public function doSwitch()
    {
        return parent::switchOperate(new QuestionsModel());
    }

    /**
     * 更新字段
     */
    public function updateField()
    {
        return parent::fieldUpdate(new QuestionsModel());
    }

    /**
     * 删除文章分类
     */
    public function delete()
    {
        $id = $this->request->param('id');
        if ($id) {
            $findCategory = QuestionsModel::get($id);

            $result = $findCategory->delete(true);
            $msg = '彻底删除';
            addLog("删除了问题ID:".$id,Db::getLastSql());

            if ($result) {
                $data['code'] = 1;
                $data['msg'] = $msg . '成功！';
            } else {
                $data['code'] = 0;
                $data['msg'] = $msg . '失败！';
            }
        } else {
            $data['code'] = 0;
            $data['msg'] = '你从哪里来？';
        }

        return $data;
    }


}
